package com.itown.expo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.web.servlet.ModelAndView;

import webframe.common.helper.ControllerHelper;

import com.itown.expo.controller.base.MyBaseCommandController;
import com.itown.expo.helper.MyControllerHelper;
import com.itown.expo.model.EntUserInfo;
import com.itown.expo.model.MainGoodsDecl;

public class EntDeclClearSearchController extends MyBaseCommandController {

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command)
			throws RuntimeException {
		EntUserInfo entUser = (EntUserInfo) ControllerHelper.getUserBySession();

		MainGoodsDecl cdecl = (MainGoodsDecl) command;
		DetachedCriteria criteria = DetachedCriteria
				.forClass(MainGoodsDecl.class);
		ControllerHelper.addEqualRestrictions(criteria, "declRegNo", entUser
				.getDeclRegNo());
		ControllerHelper.addEqualRestrictions(criteria, "id", cdecl.getId());
		ControllerHelper.addLikeRestrictions(criteria, "declNo", cdecl
				.getDeclNo());
		ControllerHelper.addLikeRestrictions(criteria, "backupCode", cdecl
				.getBackupCode());
		if (cdecl.getStatus() == null) {
			cdecl.setStatus(1L);
		}
		if (cdecl.getStatus().intValue() == 1) {
			// 所有需核销
			criteria.add(Restrictions.ge("cellation", 1L));
		} else if (cdecl.getStatus().intValue() == 2) {
			// 核销中
			ControllerHelper.addEqualRestrictions(criteria, "cellation", 1L);
		} else if (cdecl.getStatus().intValue() == 3) {
			// 完成核销
			ControllerHelper.addEqualRestrictions(criteria, "cellation", 2L);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("appPrefix", getAppPrefix());
		map.put("page", MyControllerHelper.judgeOverrun(ControllerHelper
				.queryPagination(request, criteria)));
		map.put("cdecl", cdecl);
		return new ModelAndView("EntDeclClearList.jsp", "map", map);
	}

}
